package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyRecRecordBean;
import flc.ast.databinding.ActivityIdCardRecBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.apis.base.OcrIdCardRet;
import stark.common.basic.event.EventStatProxy;

public class IdCardRecActivity extends BaseAc<ActivityIdCardRecBinding> {

    private String frontImg;
    private String reverseImg;
    public static OcrIdCardRet sIdCardRetFront;
    public static OcrIdCardRet sIdCardRetReverse;

    @Override
    protected int onCreate() {
        return R.layout.activity_id_card_rec;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivIdCardRecBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivIdCardFront.setOnClickListener(this);
        mDataBinding.ivIdCardReverse.setOnClickListener(this);

        mDataBinding.ivCopyResult.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivIdCardFront://身份证正面
                RecCameraActivity.type = 2;
                Intent intent = new Intent(this, RecCameraActivity.class);
                intent.putExtra(Content.IdCardImg, 0);
                startActivityForResult(intent, Content.ID_CARD_SEL);
                break;
            case R.id.ivIdCardReverse://身份证反面
                RecCameraActivity.type = 2;
                Intent intent1 = new Intent(this, RecCameraActivity.class);
                intent1.putExtra(Content.IdCardImg, 1);
                startActivityForResult(intent1, Content.ID_CARD_SEL);
                break;
            case R.id.ivCopyResult://复制结果
                if (sIdCardRetFront == null || sIdCardRetReverse == null) {
                    ToastUtils.showShort(getString(R.string.id_card_copy_tip));
                    return;
                }
                String content = getString(R.string.name_text) + sIdCardRetFront.words_result.name.words + "\n" + getString(R.string.gender_text) + sIdCardRetFront.words_result.gender.words + "\n" + getString(R.string.nation_text) + sIdCardRetFront.words_result.nation.words + "\n" + getString(R.string.birth_text) + sIdCardRetFront.words_result.birthDate.words + "\n" + getString(R.string.address_text) + sIdCardRetFront.words_result.address.words + "\n" + getString(R.string.id_text) + sIdCardRetFront.words_result.idNumber.words + "\n";
                ClipboardUtils.copyText(content);
                ToastUtils.showShort(getString(R.string.copy_success_tips));
        }
    }

    @Override
    protected void initData() {

    }

    private void setData() {
        if (sIdCardRetFront != null) {
            mDataBinding.tvName.setText(sIdCardRetFront.words_result.name.words == null ? "" : sIdCardRetFront.words_result.name.words);
            mDataBinding.tvGender.setText(sIdCardRetFront.words_result.gender.words == null ? "" : sIdCardRetFront.words_result.gender.words);
            mDataBinding.tvNation.setText(sIdCardRetFront.words_result.nation.words == null ? "" : sIdCardRetFront.words_result.nation.words);
            mDataBinding.tvBirth.setText(sIdCardRetFront.words_result.birthDate.words == null ? "" : sIdCardRetFront.words_result.birthDate.words);
            mDataBinding.tvAddress.setText(sIdCardRetFront.words_result.address.words == null ? "" : sIdCardRetFront.words_result.address.words);
            mDataBinding.tvId.setText(sIdCardRetFront.words_result.idNumber.words == null ? "" : sIdCardRetFront.words_result.idNumber.words);
        }
        if (sIdCardRetReverse != null) {
            if (sIdCardRetReverse.words_result.startDate == null) {
                mDataBinding.tvSign.setText("");
            } else {
                mDataBinding.tvSign.setText(sIdCardRetReverse.words_result.signOrg.words);
            }
            if (sIdCardRetReverse.words_result.startDate == null || sIdCardRetReverse.words_result.endDate.words == null) {
                mDataBinding.tvIndate.setText("");
            } else {
                mDataBinding.tvIndate.setText(sIdCardRetReverse.words_result.startDate.words + "—" + sIdCardRetReverse.words_result.endDate.words);
            }
        }
    }

    private void saveRecord() {
        List<MyRecRecordBean> listSave = new ArrayList<>();
        if (sIdCardRetFront != null) {
            String content = getString(R.string.nation_text) + sIdCardRetFront.words_result.nation.words + " " + getString(R.string.birth_text) + sIdCardRetFront.words_result.birthDate.words + " " + getString(R.string.address_text) + sIdCardRetFront.words_result.address.words + " " + getString(R.string.id_text) + sIdCardRetFront.words_result.idNumber.words;
            listSave.add(new MyRecRecordBean(frontImg, sIdCardRetFront.words_result.name.words, sIdCardRetFront.words_result.gender.words, content, TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"))));
            List<MyRecRecordBean> listSp = AppSpUtils.getRecordList();
            if (listSp != null && listSp.size() > 0) {
                listSp.addAll(listSave);
                AppSpUtils.saveRecordList(listSp);
            } else {
                AppSpUtils.saveRecordList(listSave);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Content.ID_CARD_SEL) {
                String frontPath = data.getStringExtra(Content.FrontImg);
                String reversePath = data.getStringExtra(Content.ReverseImg);
                frontImg = frontPath;
                reverseImg = reversePath;
                if (!TextUtils.isEmpty(frontPath)) {
                    Glide.with(mContext).load(frontPath).into(mDataBinding.ivIdCardFront);
                    mDataBinding.ivFrontUpload.setVisibility(View.VISIBLE);
                }
                if (!TextUtils.isEmpty(reversePath)) {
                    Glide.with(mContext).load(reversePath).into(mDataBinding.ivIdCardReverse);
                    mDataBinding.ivReverseUpload.setVisibility(View.VISIBLE);
                }
                setData();
                saveRecord();
            }
        }
    }

}